package net.expectx.pay.rpc.service.impl;

import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.base.BaseServiceImpl;
import net.expectx.pay.dao.mapper.PaymentChannelMapper;
import net.expectx.pay.dao.bean.PaymentChannel;
import net.expectx.pay.rpc.api.PaymentChannelService;
import net.expectx.pay.dao.bean.PaymentChannelExample;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * PaymentChannelService实现
 * @author lijian
 * @date 2019/7/6
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PaymentChannelServiceImpl extends BaseServiceImpl<PaymentChannel, PaymentChannelExample> implements PaymentChannelService {

    private static final Logger LOGGER = Logger.getLogger(PaymentChannelServiceImpl.class);

    @Autowired
    PaymentChannelMapper paymentChannelMapper;

    @Override
    public BaseResult insertSelective(PaymentChannel paymentChannel){
        PaymentChannelExample paymentChannelExample = new PaymentChannelExample();
        paymentChannelExample.createCriteria().andNameEqualTo(paymentChannel.getName());
        List<PaymentChannel> paymentChannels = paymentChannelMapper.selectByExample(paymentChannelExample);
        if (!paymentChannels.isEmpty()){
            PaymentChannel paymentChannel2 = paymentChannels.get(0);
            if (paymentChannel2.getIsDeleted()==0){
                return new BaseResult(false,"该渠道已存在",null);
            }
            paymentChannel2.setIsDeleted((byte) 0);
            paymentChannel2.setGmtModified(System.currentTimeMillis());
            paymentChannel2.setRate(paymentChannel.getRate());
            paymentChannel2.setPayRate(paymentChannel.getPayRate());
            paymentChannel2.setSettlementCycle(paymentChannel.getSettlementCycle());
            if(paymentChannelMapper.updateByPrimaryKeySelective(paymentChannel2)>0){
                return new BaseResult(true,"添加成功",null);
            }
        }
        if ( paymentChannelMapper.insertSelective(paymentChannel)>0){
            return new BaseResult(true,"添加成功",null);
        }
        return new BaseResult(false,"添加失败",null);
    }

}
